package com.zou.mall.platform.manage;

import com.zou.mall.platform.pojo.MailCondition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author leonard
 * @Description 邮件处理业务类
 * @date 2021-07-28 11:23
 */
@Slf4j
@Service
public class MailManager {

    @Resource
    private JavaMailSenderImpl mailSender;//注入邮件工具类

    /**
     * 发送邮件
     */
    public MailCondition sendMail(MailCondition MailCondition) {
        try {
            checkMail(MailCondition); //1.检测邮件
            sendMimeMail(MailCondition); //2.发送邮件
            return saveMail(MailCondition); //3.保存邮件
        } catch (Exception e) {
            log.error("发送邮件失败:", e);//打印错误信息
            MailCondition.setStatus("fail");
            MailCondition.setError(e.getMessage());
            return MailCondition;
        }

    }

    //检测邮件信息类
    private void checkMail(MailCondition MailCondition) {
        if (StringUtils.isEmpty(MailCondition.getTo())) {
            throw new RuntimeException("邮件收信人不能为空");
        }
        if (StringUtils.isEmpty(MailCondition.getSubject())) {
            throw new RuntimeException("邮件主题不能为空");
        }
        if (StringUtils.isEmpty(MailCondition.getText())) {
            throw new RuntimeException("邮件内容不能为空");
        }
    }

    //构建复杂邮件信息类
    private void sendMimeMail(MailCondition MailCondition) {
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);//true表示支持复杂类型
            MailCondition.setFrom(getMailSendFrom());//邮件发信人从配置项读取
            messageHelper.setFrom(MailCondition.getFrom());//邮件发信人
            messageHelper.setTo(MailCondition.getTo().split(","));//邮件收信人
            messageHelper.setSubject(MailCondition.getSubject());//邮件主题
            messageHelper.setText(MailCondition.getText());//邮件内容
            if (!StringUtils.isEmpty(MailCondition.getCc())) {//抄送
                messageHelper.setCc(MailCondition.getCc().split(","));
            }
            if (!StringUtils.isEmpty(MailCondition.getBcc())) {//密送
                messageHelper.setCc(MailCondition.getBcc().split(","));
            }
            if (MailCondition.getFiles() != null) {//添加邮件附件
                for (MultipartFile multipartFile : MailCondition.getFiles()) {
                    if (multipartFile != null && multipartFile.getOriginalFilename() != null) {
                        messageHelper.addAttachment(multipartFile.getOriginalFilename(), multipartFile);
                    }
                }
            }
            if (MailCondition.getSentDate() != null) {//发送时间
                MailCondition.setSentDate(new Date());
                messageHelper.setSentDate(MailCondition.getSentDate());
            }
            mailSender.send(messageHelper.getMimeMessage());//正式发送邮件
            MailCondition.setStatus("ok");
            log.info("发送邮件成功：{}->{}", MailCondition.getFrom(), MailCondition.getTo());
        } catch (Exception e) {
            throw new RuntimeException(e);//发送失败
        }
    }

    //保存邮件
    private MailCondition saveMail(MailCondition MailCondition) {
        //将邮件保存到数据库..
        return MailCondition;
    }

    //获取邮件发信人
    public String getMailSendFrom() {
        return mailSender.getJavaMailProperties().getProperty("from");
    }
}
